<template>
  <div class="videoContainer">
    <video class="fullscreenVideo" id="bgVid" playsinline="" autoplay="" muted="" loop="">
      <source :src="background" type="video/mp4">
    </video>
    <div class="login">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px"
               class="login-form">
        <h3 class="title">
          {{ webTitle }}
        </h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" :prefix-icon="User">
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
                    @keyup.enter.native="handleLogin" :prefix-icon="View">
          </el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
                    @keyup.enter.native="handleLogin" :prefix-icon="CircleCheckFilled">
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" alt="">
          </div>
        </el-form-item>

        <el-checkbox v-model="loginForm.rememberMe" style="margin:0 0 25px 0;">
          记住我
        </el-checkbox>


        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="default" type="primary" style="width:100%;"
                     @click.native.prevent="handleLogin">登 录
          </el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import background from '../assets/images/background.mp4'
import {CircleCheckFilled, SuccessFilled, User, View} from "@element-plus/icons-vue/global";
import {getCookie, removeCookie, setCookie} from "../utils/token";
import {codeGen, getLoginUserInfo, login} from "../api/sysAuth";
import showUtil from "../utils/alter";
import store from '../store/index'
import config from '@/enum/setting'; 
import router from '@/router';

export default {
  name: 'login',
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  computed: {
    View() {
      return View
    },
    User() {
      return User
    },
    CircleCheckFilled() {
      return CircleCheckFilled
    }
  },
  data() {
    return {
      background: background,
      // 网站标题
      webTitle: config.title,
      // 登录之后重定向页面
      redirect: undefined,
      // 登录按钮等待
      loading: false,
      // 表单校验规则
      loginRules: {
        username: [{required: true, trigger: 'blur', message: '用户名不能为空'}],
        password: [{required: true, trigger: 'blur', message: '密码不能为空'}],
        code: [{required: true, trigger: 'change', message: '验证码不能为空'}],
      },
      // 登录输入信息
      loginForm: {
        username: '',
        password: '',
        code: '',
        rememberMe: false
      },
      // 验证码显示
      codeUrl: '',

    }
  },
  created() {
    this.getLastLoginInfo()
    this.getCode()
  },
  methods: {
    // enter点击事件，登录
    handleLogin() {
      // 表单校验
      this.$refs.loginForm.validate((isValid) => {
        // 重新定义
        if (isValid) {
          this.loading = true
          let newUser = {
            username: this.loginForm.username,
            password: this.loginForm.password,
            code: this.loginForm.code,
            rememberMe: this.loginForm.rememberMe
          }
          // 勾选记住我，将数据存在cookie中
          if (newUser.rememberMe) {
            setCookie('username', newUser.username)
            setCookie('password', newUser.password)
            setCookie('rememberMe', newUser.rememberMe)
          } else {
            removeCookie('username')
            removeCookie('password')
            removeCookie('rememberMe')
          }
          // 发送登录接口
          store.dispatch('login', newUser).then(() => {
            this.loading = false
            // 路由跳转 获取用户信息在路由跳转之后出发
            router.push({path: this.redirect || '/home'})
          }).catch(() => {
            this.loading = false
            this.getCode()
            this.loginForm = {
              username: '',
              password: '',
              code: '',
              rememberMe: false
            }
          })
        } else {
          this.loading = false
          return false;
        }
      })
    },
    // 获取验证码
    getCode() {
      codeGen().then(res => {
        if (res.state) {
          let data = res.data
          this.codeUrl = 'data:image/png;base64,' + data.img
        } else {
          showUtil.showError(res.msg)
        }
      }).catch(error => {
        showUtil.showError(error)
      })
    },
    // 获取上次登录信息
    getLastLoginInfo() {
      this.loginForm.username = getCookie('username')
      this.loginForm.password = getCookie('password')
      this.loginForm.rememberMe = getCookie('rememberMe')
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 385px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  display: inline-block;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle
  }
}

.videoContainer {
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: -100;
}

.videoContainer:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  display: block;
  z-index: -1;
  top: 0;
  left: 0;
}

.fullscreenVideo {
  position: absolute;
  top: 50%;
  left: 50%;
  min-width: 100%;
  min-height: 100%;
  width: auto;
  height: auto;
  z-index: -100;
  -webkit-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
  -webkit-transition: 1s opacity;
  transition: 1s opacity;
}
</style>
